/**
 * 自定义规则验证对象
 */
class ValidateObject {

    /**
     *
     * @param ruleName 规则名称 必须唯一 建议以ID属性的值为规则名称
     * @param serviceMethod 规则验证方法
     * @param msg   不符合规则时候的错误提示
     */
    constructor(
        {
            ruleName = '',
            validate = '',
            msg = '',
        } = {},
    ) {
        this.ruleName = ruleName
        this.validate = validate
        this.msg = msg
    }
}

/**
 * 公共验证的方法
 */
class BasicValidateMethods {
    /**
     *
     * @param {RuleData} createValidateList 创建自定义规则验证对象列表
     */
    constructor({
                    createValidateList = function () {
                        if (this.isValidate) {
                            throw new Error('如果请重写 createValidateList() 方法添加自定义的验证')
                        }
                    },
                } = {}) {
        this.createValidateList = createValidateList;
    }
}

/**
 * 基本的 vue 验证数据
 */
class BasicValidateData {

    /**
     *
     * @param isValidate 是否开启验证 默认开启
     */
    constructor({
                    isValidate = true,
                } = {}) {
        this.isValidate = isValidate;
    }
}

/**
 * 基本的 vue 验证配置信息
 */
class BasicValidateOption {

    /**
     *
     * @param el
     * @param data  公共的验证数据
     * @param methods   公共的验证方法
     * @param created   公共的验证初始化操作
     */
    constructor({
                    el = '#app',
                    data = new BasicValidateData(),
                    methods = new BasicValidateMethods(),
                    created = async function () {
                        const valList = this.createValidateList();
                        if (valList) {

                            valList.forEach(({ruleName, validate, msg = '值已存在,请重填', immediate = true}) => {
                                this.$validator.extend(ruleName, {
                                    getMessage: field => msg || '值已存在,请重填',
                                    validate: async (value, args) => {
                                        return await validate(value, args);
                                    },
                                }, {
                                    immediate: immediate || true,
                                })
                            })
                        }
                    },
                } = {}) {
        this.el = el
        this.data = data
        this.methods = methods
        this.created = created
    }
}

/**
 * 验证公共操作的Vue实例
 */
class BaseValidateVue extends Vue {
    /**
     *
     * @param data
     * @param methods
     * @param mounted
     * @param args
     */
    constructor({
                    data,
                    methods,
                    mounted,
                    ...args
                } = {}) {
        super(_.merge(new BasicValidateOption(), {
            data: function () {
                return _.merge(
                    new BasicValidateData(),
                    typeof data === 'function' ? data.call(this) : data,
                )
            },
            methods,
            mounted,
            ...args,
        }));
    }

}